package net.starwifi.duobao.web.db;

import net.starwifi.duobao.AppInitListener;
import net.starwifi.duobao.entity.*;
import net.starwifi.duobao.mvc.BaseController;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.service.*;
import net.starwifi.duobao.utils.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;


/**
 * Created by Administrator on 2015/9/28.
 */
@Controller("apiDBOrderController")
@RequestMapping("/db/order")
public class OrderController extends BaseController {

    @Autowired
    DBProductService dbProductService; //夺宝产品

    @Autowired
    DBTimesService dbTimesService; //历史期

    @Autowired
    PluginService pluginService;

    @Autowired
    DBOrderService dbOrderService;

    @Autowired
    PaymentService paymentService;

    @Autowired
    MemberService memberService;

    @Autowired
    SettingService settingService;

    /**
     * 提交订单
     */
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public String submit(Model model, Long productId, String timesNo, Integer num, HttpServletRequest request) {
        Member member = memberService.getCurrent();

        // 夺宝产品
        DBProduct dbProduct = dbProductService.find(productId);
        if (!member.getIsEnabled()) {
            model.addAttribute("content", "账户已禁用，请联系客服");
            return "/db/error";
        }
        if (dbProduct == null) { //产品存在
            model.addAttribute("content", "该产品不存在");
            return "/db/error";
        }
        if (num == null) {
            model.addAttribute("content", "购买份数不能为空");
            return "/db/error";
        }
        if (num < 1) {
            model.addAttribute("content", "购买份数不能小于1");
            return "/db/error";
        }

        // 夺宝期
        DBTimes dbTimes = dbTimesService.findByTimesNo(timesNo);
        if (dbTimes == null) { //已经结束的不能购买
            model.addAttribute("content", "该夺宝期不存在");
            return "/db/error";
        }

        if (num < dbTimes.getStepNum()) {
            model.addAttribute("content", "购买份数不能小于：" + dbTimes.getStepNum() + "份");
            return "/db/error";
        }
        if (num % dbTimes.getStepNum() != 0) {
            model.addAttribute("content", "必须是：" + dbTimes.getStepNum() + "的整数倍");
            return "/db/error";
        }
        if (num > dbTimes.getPurchaseQuantity()) {
            model.addAttribute("content", "超过数量限制");
            return "/db/error";
        }

        // 创建商户系统订单
        String orderSn = AppInitListener.idGenerate.getID().toString();
        String ip = IpUtils.getIpAddr(request);
        String ipAddress = IpUtils.getIpAddress(ip);

        final DBOrder order = dbOrderService.build(dbProduct, orderSn, timesNo, num, ip, ipAddress, "");
        if(order == null) {
            model.addAttribute("content", "提交订单失败,请稍后再试!");
            return "/db/error";
        }

        return "redirect:/db/order/pay?id=" + order.getId();
    }


    /**
     * 订单支付
     */
    @RequestMapping(value = "/pay", method = RequestMethod.GET)
    public String db(Model model, Long id) {
        Member member = memberService.getCurrent();

        // 夺宝订单
        DBOrder dbOrder = dbOrderService.find(id);
        if (dbOrder == null) {
            model.addAttribute("content", "订单不存在");
            return "/db/error";
        }
        if (dbOrder.getOrderStatus() == 1 || dbOrder.getOrderStatus() == 2) {
            model.addAttribute("content", "订单已完成,无法再支付");
            return "/db/error";
        }
        if (dbOrder.getPaymentStatus() == 1) {
            model.addAttribute("content", "订单已支付,无法再继续支付");
            return "/db/error";
        }
        if (!dbOrder.getMemberId().equals(member.getId())) {
            model.addAttribute("content", "非法操作");
            return "/db/error";
        }

        // 夺宝产品
        DBProduct dbProduct = dbProductService.find(dbOrder.getProductId());
        if (dbProduct == null) { //产品存在
            model.addAttribute("content", "该产品不存在");
            return "/db/error";
        }
        if(Boolean.FALSE.equals(dbProduct.getIsMarketable())) {
            model.addAttribute("content", "该产品不存在");
            return "/db/error";
        }

        // 夺宝期
        DBTimes dbTimes = dbTimesService.findByTimesNo(dbOrder.getTimesNo());
        if (dbTimes == null) { //已经结束的不能购买
            model.addAttribute("content", "该夺宝期不存在");
            return "/db/error";
        }
        if (dbTimes.getIsOver()) {
            model.addAttribute("content", "该期夺宝已经结束");
            return "/db/error";
        }

        // 购买分数
        Integer num = dbOrder.getQuantity();
        if (num == null) {
            model.addAttribute("content", "购买份数不能为空");
            return "/db/error";
        }
        if (num < 1) {
            model.addAttribute("content", "购买份数不能小于1");
            return "/db/error";
        }
        if (num < dbTimes.getStepNum()) {
            model.addAttribute("content", "购买份数不能小于：" + dbTimes.getStepNum() + "份");
            return "/db/error";
        }
        if (num % dbTimes.getStepNum() != 0) {
            model.addAttribute("content", "必须是：" + dbTimes.getStepNum() + "的整数倍");
            return "/db/error";
        }
        if (num > dbTimes.getPurchaseQuantity()) {
            model.addAttribute("content", "超过数量限制");
            return "/db/error";
        }
        //检查库存
        if (num > (dbTimes.getQuantity() - dbTimes.getCurrentQuantity())) {
            model.addAttribute("content", "仅剩下" + (dbTimes.getQuantity() - dbTimes.getCurrentQuantity()) + "份库存");
            return "/db/error";
        }

        model.addAttribute("member", member);
        model.addAttribute("dbOrder", dbOrder);
        model.addAttribute("dbProduct", dbProduct);
        model.addAttribute("dbTimes", dbTimes);

        return "/db/pay-order";
    }

}
